@namespace Oqtane.Modules.Admin.Logs
@using System.Globalization
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject ILogService LogService
@inject IPageService PageService
@inject IPageModuleService PageModuleService
@inject IUserService UserService
@inject IStringLocalizer<Detail> Localizer
@inject IStringLocalizer<SharedResources> SharedLocalizer

@if (_initialized)
{
	<div class="container">
		<div class="row mb-1 align-items-center">
			<Label Class="col-sm-3" For="dateTime" HelpText="The date and time of this log" ResourceKey="DateTime">Date/Time: </Label>
			<div class="col-sm-9">
				<input id="dateTime" class="form-control" @bind="@_logDate" readonly />
			</div>
		</div>
		<div class="row mb-1 align-items-center">
			<Label Class="col-sm-3" For="level" HelpText="The level of this log" ResourceKey="Level">Level: </Label>
			<div class="col-sm-9">
				<input id="level" class="form-control" @bind="@_level" readonly />
			</div>
		</div>
		<div class="row mb-1 align-items-center">
			<Label Class="col-sm-3" For="feature" HelpText="The feature that was affected" ResourceKey="Feature">Feature: </Label>
			<div class="col-sm-9">
				<input id="feature" class="form-control" @bind="@_feature" readonly />
			</div>
		</div>
		<div class="row mb-1 align-items-center">
			<Label Class="col-sm-3" For="function" HelpText="The function that was performed" ResourceKey="Function">Function: </Label>
			<div class="col-sm-9">
				<input id="function" class="form-control" @bind="@_function" readonly />
			</div>
		</div>
		<div class="row mb-1 align-items-center">
			<Label Class="col-sm-3" For="category" HelpText="The fully qualified type type that was affected" ResourceKey="Category">Type Name: </Label>
			<div class="col-sm-9">
				<input id="category" class="form-control" @bind="@_category" readonly />
			</div>
		</div>
		@if (_pageName != string.Empty)
		{
			<div class="row mb-1 align-items-center">
				<Label Class="col-sm-3" For="page" HelpText="The page that was affected" ResourceKey="Page">Page: </Label>
				<div class="col-sm-9">
					<input id="page" class="form-control" @bind="@_pageName" readonly />
				</div>
			</div>
		}
		@if (_moduleTitle != string.Empty)
		{
			<div class="row mb-1 align-items-center">
				<Label Class="col-sm-3" For="module" HelpText="The module that was affected" ResourceKey="Module">Module: </Label>
				<div class="col-sm-9">
					<input id="module" class="form-control" @bind="@_moduleTitle" readonly />
				</div>
			</div>
		}
		@if (_username != string.Empty)
		{
			<div class="row mb-1 align-items-center">
				<Label Class="col-sm-3" For="user" HelpText="The user that caused this log" ResourceKey="User">User: </Label>
				<div class="col-sm-9">
					<input id="user" class="form-control" @bind="@_username" readonly />
				</div>
			</div>
		}
		<div class="row mb-1 align-items-center">
			<Label Class="col-sm-3" For="url" HelpText="The url the log comes from" ResourceKey="Url">Url: </Label>
			<div class="col-sm-9">
				<input id="url" class="form-control" @bind="@_url" readonly />
			</div>
		</div>
		<div class="row mb-1 align-items-center">
			<Label Class="col-sm-3" For="template" HelpText="What the log is about" ResourceKey="Template">Template: </Label>
			<div class="col-sm-9">
				<input id="template" class="form-control" @bind="@_template" readonly />
			</div>
		</div>
		<div class="row mb-1 align-items-center">
			<Label Class="col-sm-3" For="message" HelpText="The message that the system generated" ResourceKey="Message">Message: </Label>
			<div class="col-sm-9">
				<textarea id="message" class="form-control" @bind="@_message" rows="5" readonly></textarea>
			</div>
		</div>
		@if (!string.IsNullOrEmpty(_exception))
		{
			<div class="row mb-1 align-items-center">
				<Label Class="col-sm-3" For="exception" HelpText="The exceptions generated by the system" ResourceKey="Exception">Exception: </Label>
				<div class="col-sm-9">
                    <textarea id="exception" class="form-control" @bind="@_exception" rows="5" readonly></textarea>
                </div>
           </div>
        }
       <div class="row mb-1 align-items-center">
            <Label Class="col-sm-3" For="properties" HelpText="The properties that were affected" ResourceKey="Properties">Properties: </Label>
            <div class="col-sm-9">
                <textarea id="properties" class="form-control" @bind="@_properties" rows="5" readonly></textarea>
            </div>
       </div>
       <div class="row mb-1 align-items-center">
            <Label Class="col-sm-3" For="server" HelpText="The server that was affected" ResourceKey="Server">Server: </Label>
			<div class="col-sm-9">
                <input id="server" class="form-control" @bind="@_server" readonly />
            </div>
        </div>
    </div>
}
<NavLink class="btn btn-secondary" href="@CloseUrl()">@SharedLocalizer["Cancel"]</NavLink>

@code {
	private bool _initialized = false;
    private int _logId;
    private string _logDate = string.Empty;
    private string _level = string.Empty;
    private string _feature = string.Empty;
    private string _function = string.Empty;
    private string _category = string.Empty;
    private string _pageName = string.Empty;
    private string _moduleTitle = string.Empty;
    private string _username = string.Empty;
    private string _url = string.Empty;
    private string _template = string.Empty;
    private string _message = string.Empty;
    private string _exception = string.Empty;
    private string _properties = string.Empty;
    private string _server = string.Empty;

	public override string UrlParametersTemplate => "/{id}";
	public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            _logId = Int32.Parse(UrlParameters["id"]);
            var log = await LogService.GetLogAsync(_logId);
            if (log != null)
            {
                _logDate = log.LogDate.ToString(CultureInfo.CurrentCulture);
                _level = log.Level;
                _feature = log.Feature;
                _function = log.Function;
                _category = log.Category;

                if (log.PageId != null)
                {
                    var page = await PageService.GetPageAsync(log.PageId.Value);
                    if (page != null)
                    {
                        _pageName = page.Name;
                    }
                }

                if (log.PageId != null && log.ModuleId != null && log.ModuleId != -1)
                {
                    var pagemodule = await PageModuleService.GetPageModuleAsync(log.PageId.Value, log.ModuleId.Value);
                    if (pagemodule != null)
                    {
                        _moduleTitle = pagemodule.Title;
                    }
                }

                if (log.UserId != null)
                {
                    var user = await UserService.GetUserAsync(log.UserId.Value, PageState.Site.SiteId);
                    if (user != null)
                    {
                        _username = user.Username;
                    }
                }

                _url = log.Url;
                _template = log.MessageTemplate;
                _message = log.Message;
                _exception = log.Exception;
                _properties = log.Properties;
                _server = log.Server;
				_initialized = true;
            }
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Loading Log {LogId} {Error}", _logId, ex.Message);
            AddModuleMessage(Localizer["Error.Log.Load"], MessageType.Error);
        }
    }

	private string CloseUrl()
	{
		return (!string.IsNullOrEmpty(PageState.ReturnUrl)) ? PageState.ReturnUrl : NavigateUrl();
	}
}
